//
//  PerfectViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/17.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//  完善信息页面

#import "PerfectViewController.h"
#import "PerfectView.h"
#import "Perfect1ViewController.h"
#import "ZLPhotoPickerViewController.h"
#import "UIImage+ZLPhotoLib.h"
#import "ZLPhotoPickerBrowserPhoto.h"
#import "MOFSPickerManager.h"
@interface PerfectViewController ()

@property(nonatomic,weak)PerfectView *perfectView;

@property (nonatomic , strong) ZLPhotoPickerBrowserPhoto *photo;

@end

@implementation PerfectViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    PerfectView *perfectView = [[PerfectView alloc] initWithFrame:self.view.frame];
    
    [perfectView.nextBtn addTarget:self action:@selector(nextBtnClick)];
    
    [perfectView.photoBtn addTarget:self action:@selector(photoSelect:)];
    
    [perfectView.typeBtnView.choseBtn addTarget:self action:@selector(typeSelect)];
    
    [self.view addSubview:perfectView];
    
    self.perfectView = perfectView;
}


-(void)photoSelect:(UIButton *)photo{
    
    ZLPhotoPickerViewController *pickerVc = [[ZLPhotoPickerViewController alloc] init];
    
    pickerVc.maxCount = 1;
    
    // Jump AssetsVc
    pickerVc.status = PickerViewShowStatusCameraRoll;
    // Recoder Select Assets
    //    pickerVc.selectPickers = self.assets;
    // Filter: PickerPhotoStatusAllVideoAndPhotos, PickerPhotoStatusVideos, PickerPhotoStatusPhotos.
    
    pickerVc.photoStatus = PickerPhotoStatusPhotos;
    // Desc Show Photos, And Suppor Camera
    pickerVc.topShowPhotoPicker = YES;
    
    // CallBack
    __weak typeof(self)weakSelf = self;
    
    pickerVc.callBack = ^(NSArray<ZLPhotoAssets *> *status){
        
        for (ZLPhotoAssets *assets in status) {
            
            ZLPhotoPickerBrowserPhoto *photo = [ZLPhotoPickerBrowserPhoto photoAnyImageObjWith:assets];
            
            photo.asset = assets;
            
            weakSelf.photo = photo;
            
            //拍照
            if ([photo.asset isKindOfClass:[UIImage class]]) {
                
                [weakSelf.perfectView.photoBtn setImage:(UIImage *)photo.asset forState:UIControlStateNormal];
            }
            else{
                
                [weakSelf.perfectView.photoBtn setImage:[assets thumbImage] forState:UIControlStateNormal];
                
            }
            
            break;
        }
    };
    
    [pickerVc showPickerVc:self];
    
}


#pragma mark 教练还是师傅
-(void)typeSelect{
    
    [self.view endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    [[MOFSPickerManager shareManger] showPickerViewWithDataArray:@[@"师傅",@"教练"] tag:67 title:@"职位选择" cancelTitle:@"取消" commitTitle:@"确定" commitBlock:^(NSString *string) {
        
        weakSelf.perfectView.typeBtnView.choseLabel.text = string;
        
        weakSelf.perfectView.typeBtnView.choseLabel.textColor = TFColor;
        
        
    } cancelBlock:^{
        
    }];
}

-(void)nextBtnClick{
    
    
    NSString *nickName = self.perfectView.nameTF.text;
    
    NSString *realName = self.perfectView.realNameTF.text;
    
    if (!nickName || [nickName isEmptyString] || [realName isEmptyString]) {
        
        [MBProgressHUD showText:@"昵称和真实姓名不能为空" toview:self.view];
        
        return;
    }
    
    UserAccount *userAccount = [UserAccount account];
    
    userAccount.NickName = nickName;
    
    userAccount.RealName = realName;
    
    userAccount.MasterType = [self.perfectView.typeBtnView.choseLabel.text isEqualToString:@"师傅"]?@"1":@"0";
    
    [UserAccount saveAccount:userAccount];
    
    
    
    NSData *imageData;
    
    UIImage *image;
    
    if ([self.photo.asset isKindOfClass:[UIImage class]]) {
        
        image = (UIImage *)self.photo.asset;
    }
    else{
        
        image = [self.photo.asset originImage];
    }
    
    
    
    if (!image) {

        UserAccount *userAccount = [UserAccount account];

        userAccount.Pic = @"";

        [UserAccount saveAccount:userAccount];

        Perfect1ViewController *perfectVC = [[Perfect1ViewController alloc] init];

        [self.navigationController pushViewController:perfectVC animated:YES];

        return;
    }

//    [MBProgressHUD showMessage:@"上传中..." toView:self.view];

    imageData  = UIImageJPEGRepresentation(image,0.1);

    RAYUploader *upload = [RAYUploader defaultUploader];

    [upload uploadWithData:imageData complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {

        [MBProgressHUD hideHUDForView:self.view];

        //说明上传成功
        if (key) {

            dispatch_async(dispatch_get_main_queue(), ^{

                //Update UI in UI thread here
                NSString *picUrl = [NSString stringWithFormat:@"%@%@",QiNiuBase,key];

                UserAccount *userAccount = [UserAccount account];

                userAccount.Pic = picUrl;

                [UserAccount saveAccount:userAccount];

                Perfect1ViewController *perfectVC = [[Perfect1ViewController alloc] init];

                [self.navigationController pushViewController:perfectVC animated:YES];
            });
        }

        NSLog(@"%@", info);

        NSLog(@"%@", resp);

    }];
}
@end
